<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function insert_order($sdk_order){
		$this->db->trans_start();

		foreach($sdk_order as $k=>$v){
			if ($k != 'goodslist') $so[$k] = $v;
		}
		$so['create_time'] = time();
		$sql = $this->db->insert_string('sdk_order', $so);
		
		$this->db->query($sql);
		
		$orderid = $this->db->insert_id();
		
		foreach($sdk_order->goodslist as $v){
			$v->orderid = $orderid;
			$sql = $this->db->insert_string('sdk_product', $v);
			
			$this->db->query($sql);
		}
		
		$this->db->trans_complete();
		
		return $orderid;
	}
	
	function order_status($sdk_order){
		
		$sql = "update sdk_order set status='$sdk_order->status',order_sn='$sdk_order->transactionid' where orderid='$sdk_order->orderid'";
	
		$sql = $this->db->query($sql);
		
		return $this->db->affected_rows() > 0 ? 0 : -1; 
	}
        
        function order_status2($sdk_order){
		
		$sql = "update sdk_order set status='$sdk_order->status',order_sn='$sdk_order->transactionid' where ordersn='$sdk_order->ordersn'";
	
		$sql = $this->db->query($sql);
		
		return $this->db->affected_rows() > 0 ? 0 : -1; 
	}
	
	 function order_list($sdk_order){
	 	$sql = "select * from sdk_order where uid= ? and siteid = ? order by create_time desc limit " . ($sdk_order->index - 1) * $sdk_order->pagesize . ", " . $sdk_order->pagesize;
		return $this->db->query($sql, array('uid'=>$sdk_order->uid, 'siteid'=>$sdk_order->siteid));
	 }
	 
	 function ordernumrows($where=''){
	 	
	 	$q = $this->db->query("select * from sdk_order".$where);
	 
	 	return $q->num_rows();
	 
	 }
	 
	 function get_order_list($limit,$size,$where=''){	
	 	$sql = "select o.*,m.*,s.sitename from sdk_order o LEFT join sdk_member m on o.uid=m.mid left JOIN sdk_site s on o.siteid = s.siteid ".$where." order by create_time desc limit ".$limit*$size.",".$size;
		
		$q = $this->db->query($sql);
		
		return $q->result();
	 }
	 
	 function order_detail($orderid){
	 	$sql = "select * from sdk_order where orderid =" . $orderid . ' limit 1';
		$q = $this->db->query($sql);
		
		$sdk_order = $q->first_row();
		
		$sql = "select * from sdk_member where mid=" . $sdk_order->uid . ' limit 1';
		$q = $this->db->query($sql);
		$sdk_member = $q->first_row();
		
		$sql = "select * from sdk_site where siteid=" . $sdk_order->siteid . ' limit 1';
		$q = $this->db->query($sql);
		$sdk_site = $q->first_row();
		
		$sql = "select * from sdk_product where orderid=" . $sdk_order->orderid;
		$q = $this->db->query($sql);
		$sdk_product = $q->result();
		
		$a['order'] = $sdk_order;
		$a['member'] = $sdk_member;
		$a['site'] = $sdk_site;
		$a['product'] = $sdk_product;
		
		
		return $a;
	 }
	 
	 function order_count($sdk_order=NULL)
	 {
             if(!$sdk_order){
	 	return $this->db->count_all('sdk_order');
             }else{
                 $sql = "select * from sdk_order where uid= ? and siteid = ? ";
		 $q= $this->db->query($sql, array('uid'=>$sdk_order->uid, 'siteid'=>$sdk_order->siteid));
             
                 return count($q->result());
             }
	 }
	 
	 function order_products($sdk_order){
	 	$sql = "select * from sdk_product where orderid = " . $sdk_order->orderid; 
		$q = $this->db->query($sql);
		return $q->result();	
	 }	
	 
	 function order_info($sdk_order){
	 	$sql = "select * from sdk_order where orderid = " . $sdk_order->orderid;
	 	$q = $this->db->query($sql);
	 	return $q->row();
	 }
	 
         function order_info2($sdk_order){
	 	$sql = "select * from sdk_order where ordersn = " . $sdk_order->ordersn;
	 	$q = $this->db->query($sql);
	 	return $q->row();
	 }
         
	 function delorder($orderid=''){
	 	 
	 	$this->db->delete('sdk_order',array('orderid'=>$orderid));
	 	if($this->db->affected_rows()>0){
	 		$this->db->delete('sdk_product',array('orderid'=>$orderid));
	 		return true;
	 	}else{
	 		return false;
	 	}
	 	 
	 }
	 
	 function updatestatus($orderid='',$status=''){
	 	 
	 	$this->db->update('sdk_order',array('status'=>$status),array('orderid'=>$orderid));
	 	if($this->db->affected_rows()>0){
	 		return true;
	 	}else{
	 		return false;
	 	}
	 	
	 }
	 
	 function updateshipstatus($orderid='',$shipstatus='',$shipno=""){
	 
	 	return $this->db->update('sdk_order',array('shipstatus'=>$shipstatus,'shipno'=>$shipno),array('orderid'=>$orderid));
	 }
	 
	 function updatechengben($orderid='',$chengben=''){
	 
	 	return $this->db->update('sdk_order',array('chengben'=>$chengben),array('orderid'=>$orderid));
	 }
	 
	 function updatebeizhu($orderid='',$beizhu=''){
	 
	 	return $this->db->update('sdk_order',array('beizhu'=>$beizhu),array('orderid'=>$orderid));
	 }
	 
	 
	 function get_orderids($uid=''){
	 	 
	 	$sql = "select orderid from sdk_order where uid='$uid' order by create_time desc";
	 	$q = $this->db->query($sql);
	 	return $q->result();
	 	 
	 }
}
?>